package com.xy.blog.article.domain.dao.impl;

import com.github.pagehelper.PageHelper;
import com.xy.blog.article.domain.dao.IArticleTagDao;
import com.xy.blog.article.domain.dao.mapper.ArticleTagMapper;
import com.xy.blog.article.domain.dao.mapper.ArticleTagRelMapper;
import com.xy.blog.article.domain.dao.model.ArticleTagDO;
import com.xy.blog.article.domain.dao.model.ArticleTagRelDO;
import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.id.SnowflakeIdWorker;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.core.springmvc.result.page.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/5/27
 */
@Repository
public class ArticleTagDaoImpl implements IArticleTagDao {

    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Autowired
    private ArticleTagRelMapper articleTagRelMapper;

    @Override
    public PageResp<ArticleTagDO> listByPage(PageReq<String> req) throws BaseException {

        Example example = new Example(ArticleTagDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (req.getParams() != null) {
            criteria.andLike("name", "%" + req.getParams().trim() + "%");
        }
        PageHelper.startPage(req);
        List<ArticleTagDO> articleTagDOS = articleTagMapper.selectByExample(example);
        return PageResp.data(articleTagDOS);
    }

    @Override
    public List<ArticleTagDO> select(ArticleTagDO item) {
        return articleTagMapper.select(item);
    }

    @Override
    public int selectCount(ArticleTagDO item) {
        return articleTagMapper.selectCount(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ArticleTagDO item) throws BaseException {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setCreateTime(now);
        }
        if (item.getUpdateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setUpdateTime(now);
        }
        articleTagMapper.insertSelective(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(ArticleTagDO item) throws BaseException {
        item.setUpdateTime(LocalDateTime.now());
        articleTagMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) throws BaseException {
        articleTagMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ArticleTagDO getById(Long id) throws BaseException {
        return articleTagMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRel(ArticleTagRelDO item) {
        if (item.getId() == null) {
            item.setId(SnowflakeIdWorker.generateId());
        }
        if (item.getCreateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setCreateTime(now);
        }
        if (item.getUpdateTime() == null) {
            LocalDateTime now = LocalDateTime.now();
            item.setUpdateTime(now);
        }
        articleTagRelMapper.insertSelective(item);
    }

    @Override
    public List<ArticleTagRelDO> selectRel(ArticleTagRelDO item) {
        return articleTagRelMapper.select(item);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelByTagId(Long id) {
        ArticleTagRelDO articleTagRelDO = new ArticleTagRelDO();
        articleTagRelDO.setTagId(id);
        articleTagRelMapper.delete(articleTagRelDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelByArticleId(Long id) {
        ArticleTagRelDO articleTagRelDO = new ArticleTagRelDO();
        articleTagRelDO.setArticleId(id);
        articleTagRelMapper.delete(articleTagRelDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelById(Long id) {
        articleTagRelMapper.deleteByPrimaryKey(id);
    }


}
